package cn.cvzhanshi.wechatpush.utils;

import cn.cvzhanshi.wechatpush.entity.Weather;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author yupeng
 * @date 2022/8/23 13:52
 * @return
 */
public class WeatherUtils {
    public static void main(String[] args) {
        System.out.println(getWeather("北京"));
    }
    private static final String appId = "lhcqhpmvspggdpiq";
    private static final String app_secret = "cUtwcDVnQTkwdXBaRm81cEEraDd2QT09";
    /**
     * 获取天气信息
     * @param cityName
     * @author yupeng
     * @date 2022/8/23 13:52
     * @return cn.cvzhanshi.wechatpush.entity.Weather
     */
    public static Weather getWeather(String cityName){
        RestTemplate restTemplate = new RestTemplate();
        String res = restTemplate.getForObject(
                "https://www.mxnzp.com/api/weather/forecast/" + cityName + "?app_id=" + appId + "&app_secret=" + app_secret,
                String.class);
        System.out.println("getWeather:"+res);
        JSONObject json = JSONObject.parseObject(res);
        JSONArray forecasts = json.getJSONObject("data").getJSONArray("forecasts");
        List<Weather> weathers = forecasts.toJavaList(Weather.class);
        Weather weather = weathers.get(0);
        weather.setDayOfWeek(getDayOfWeek(weather.getDayOfWeek()));
        weather.setAddress(json.getJSONObject("data").getString("address"));
        weather.setCityCode(json.getJSONObject("data").getString("cityCode"));
        return weather;
    }
    /**
     *
     * 获取每日一句
     * @author yupeng
     * @date 2022/8/23 13:52
     * @return java.lang.String
     */
    public static String getRecommend(){
        RestTemplate restTemplate = new RestTemplate();
        String res = restTemplate.getForObject(
                "https://www.mxnzp.com/api/daily_word/recommend?count=1&app_id=" + appId + "&app_secret=" + app_secret,
                String.class);
        System.out.println("getRecommend:"+res);
        JSONObject json = JSONObject.parseObject(res);
        JSONArray forecasts = json.getJSONArray("data");
        return forecasts.getJSONObject(0).getString("content");
    }

    /**
     *
     * 星期 数字转汉字
     * @param str
     * @author yupeng
     * @date 2022/8/23 13:52
     * @return java.lang.String
     */
    private static String getDayOfWeek(String str) {
        switch (str) {
            case "1":
                str = "一";
                break;
            case "2":
                str = "二";
                break;
            case "3":
                str = "三";
                break;
            case "4":
                str = "四";
                break;
            case "5":
                str = "五";
                break;
            case "6":
                str = "六";
                break;
            case "7":
                str = "七";
                break;
        }
        return str;
    }
}
